package principal;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import outros.Documento;
import persistencia.DAOUsuario;
import transporte.TOUsuario;

public class Login extends javax.swing.JFrame {
    private DAOUsuario dao;
    private TOUsuario to;
    private JFrame pai;

    public Login() {
        initComponents();
        dao = new DAOUsuario();
        to = new TOUsuario();
    }

    public void setPai(JFrame pai) {
        this.pai = pai;
    }

    private boolean montaLogin() {
        if (!camNome.getText().isEmpty()) {
            if (!camSenha.getText().isEmpty()) {
                to.setNome(camNome.getText());
                to.setSenha(camSenha.getText());
                return true;
            } else {
                camSenha.requestFocus();
                JOptionPane.showMessageDialog(null, "Preencha o campo senha.");
                return false;
            }
        } else {
            camNome.requestFocus();
            JOptionPane.showMessageDialog(null, "Preencha o campo nome.");
            return false;
        }
    }

    @SuppressWarnings("static-access")
    private void logar() {
        if (montaLogin()) {
            TOUsuario temp = dao.logar(camNome.getText(), camSenha.getText());
            if (temp!=null) {
                //JOptionPane.showMessageDialog(null, "Logado com sucesso. Bem vindo "+temp.getNome()+".");
                ((Principal)pai).setUsuarioLogado(temp);
                camNome.setText("");
                camSenha.setText("");
                pai.setVisible(true);
                dispose();
            } else {
                JOptionPane.showMessageDialog(null, "Nome ou senha inválidos...");
            }
        }
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        camNome = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        camSenha = new javax.swing.JPasswordField();
        botLogar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Patrimônio - Login");
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel1.setText("Nome");

        camNome.setDocument(new Documento(false, 20, true));
        camNome.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        camNome.setText("MARCIO.LINO");
        camNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                camNomeActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel2.setText("Senha");

        camSenha.setDocument(new Documento(false, 20, false));
        camSenha.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        camSenha.setText("segredo");
        camSenha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                camSenhaActionPerformed(evt);
            }
        });

        botLogar.setText("Logar");
        botLogar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botLogarActionPerformed(evt);
            }
        });

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/teclado.jpg"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(camNome, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(camSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(botLogar, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap(10, Short.MAX_VALUE))
            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 210, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 148, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(camNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(camSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botLogar)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-218)/2, (screenSize.height-280)/2, 218, 280);
    }// </editor-fold>//GEN-END:initComponents

    private void botLogarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botLogarActionPerformed
        logar();
}//GEN-LAST:event_botLogarActionPerformed

    private void camNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_camNomeActionPerformed
        logar();
    }//GEN-LAST:event_camNomeActionPerformed

    private void camSenhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_camSenhaActionPerformed
        logar();
    }//GEN-LAST:event_camSenhaActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Login().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botLogar;
    public static javax.swing.JTextField camNome;
    public static javax.swing.JTextField camSenha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration//GEN-END:variables

}
